﻿using Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using FluentAssertions;

namespace WikiBugtracker.Test
{
    
    
    /// <summary>
    ///This is a test class for SettingsManagerTest and is intended
    ///to contain all SettingsManagerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class SettingsManagerTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for SettingsManager Constructor
        ///</summary>
        [TestMethod()]
        public void SettingsManager_Constructor_ToString_Test()
        {
            String parameters = "KEY1=Value1;";
            SettingsManager target = new SettingsManager(parameters);

            parameters.Should().Be(target.ToString());
            target["KEY1"].Should().Be("Value1");
            target["KEY2"].Should().BeNull();
        }

        /// <summary>
        ///A test for Add
        ///</summary>
        [TestMethod()]
        public void SettingsManager_Set_Test()
        {
            SettingsManager target = new SettingsManager();
            target.Add("KEY1");

            String parameters = "KEY1=Value1;";
            target.Set(parameters);

            parameters.Should().Be(target.ToString());
        }

        [TestMethod()]
        public void SettingsManager_Set_Parameter_Multiple_Test()
        {
            SettingsManager target = new SettingsManager();

            int index = 0;
            string value = "Value" + (++index).ToString();
            String parameters = string.Format("KEY1={0};", value);

            target.Add("KEY1");
            target["KEY1"].Should().BeNullOrEmpty();
            target["KEY2"].Should().BeNull();
            
            target["KEY1"] = value;
            target.ToString().Should().Be(parameters);
            target["KEY1"].Should().Be(value);

            target.Keys.Should().Contain("KEY1").And.NotContain("KEY");
        }

        [TestMethod()]
        public void SettingsManager_Set_Encrypted_Parameter_Multiple_Test()
        {
            SettingsManager target = new SettingsManager();

            int index = 1;
            string value = "Value" + (++index).ToString();
            String parameters = string.Format("KEY1={0};", value);
            target.Add("KEY1", string.Empty, true);
            target["KEY1"].Should().BeNullOrEmpty();
            target["KEY2"].Should().BeNull();
            
            value = "Value" + (++index).ToString();
            parameters = string.Format("KEY1={0};", value);
            target.Add("KEY1", new EncryptedParameter(string.Empty));
            target.ToString().Should().BeNullOrEmpty();
            target["KEY1"].Should().NotBe(value);

            target["KEY1"] = value;
            target.ToString().Should().NotBe(parameters);
            target["KEY1"].Should().Be(value);

            target.Keys.Should().Contain("KEY1").And.NotContain("KEY");
        }
    }
}
